package com.yyfax.common.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.yyfax.common.R;
import com.yyfax.common.tools.EditTextInputFilter;


/**
 * 手机号码, 密码输入框
 */
//todo 增大del按钮的点击范围, 设置paddingLeft
public class LoginLayout extends LinearLayout implements CompoundButton.OnCheckedChangeListener {
	private static final String TAG = "LoginLayout";

	private PwdEditText mEditText;
	private CheckBox mShowPwdCb;
	/**类型 1:登录框 2:密码框*/
	private int type;
	private String hint;  //hintText
	/**
	 * 设置左边drawable
	 */
	private Drawable mLeftDrawable;
	/**
	 * 设置右边EditText的inputType
	 */
	private int mInputType;

	public void addTextChangedListener(TextWatcher watcher) {
		mEditText.addTextChangedListener(watcher);
	}

	public LoginLayout(Context context) {
		super(context);
	}

	public LoginLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public LoginLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	private void init(final Context context, AttributeSet attrs) {
		// setBackgroundColor(context.getResources().getColor(android.R.color.white));
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.phone_layout, this);
		mEditText = (PwdEditText) view.findViewById(R.id.edit_text);
		//mEditText.addTextChangedListener(this);

		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FormRow);

		mLeftDrawable = ta.getDrawable(R.styleable.FormRow_android_drawableLeft);
		if (mLeftDrawable != null) {//设置drawableLeft
			mLeftDrawable.setBounds(0, 0, mLeftDrawable.getMinimumWidth(), mLeftDrawable.getMinimumHeight());
		}
		mEditText.setCompoundDrawables(mLeftDrawable, null, mEditText.getCompoundDrawables()[2], null);
		mEditText.updateDrawables();

		hint = ta.getString(R.styleable.FormRow_hint);
		mEditText.setHint(hint);

		int maxLength = ta.getInt(R.styleable.FormRow_maxLength, -1);
		if (maxLength >= 0) {
			mEditText.setFilters(new InputFilter[]{new EditTextInputFilter(),new InputFilter.LengthFilter(maxLength)});
		} else {
			mEditText.setFilters(new InputFilter[]{new EditTextInputFilter()});
		}

		mInputType = ta.getInt(R.styleable.FormRow_android_inputType, EditorInfo.TYPE_NULL);
		if (mInputType != EditorInfo.TYPE_NULL)
			mEditText.setInputType(mInputType);

		type = ta.getInt(R.styleable.FormRow_customType, 1);
		if (type == 2) {
			mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());//默认是不可见的
			ViewStub viewStub = (ViewStub) view.findViewById(R.id.show_pwd_stub);
			viewStub.inflate();
			mShowPwdCb = (CheckBox) view.findViewById(R.id.cb_show);
			mShowPwdCb.setOnCheckedChangeListener(this);
			mEditText.setTextColor(getResources().getColor(R.color.color_333333));	//设置字体颜色
		}
		mEditText.setTextColor(getResources().getColor(R.color.color_333333));	//设置字体颜色
		mEditText.setTextSize(15);
		ta.recycle();
	}

	public Editable getText() {//获得输入内容
		return mEditText.getText();
	}

	public void setText(CharSequence text) {
		mEditText.setText(text);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {//设置密码可见性
			mEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//设置EditText的内容为可见
		} else {
			mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置EditText的内容为不可见
		}
		mEditText.setSelection(mEditText.getText().toString().length());//焦点移动到最后
		mEditText.postInvalidate();
	}
}
